package models

import (
	"log"
	"time"
)

// Menu 菜单模型
type Menu struct {
	Id         int       `xorm:"pk autoincr" json:"id"`
	Name       string    `xorm:"varchar(50) notnull" json:"name" binding:"required"`
	Icon       string    `xorm:"default 0" json:"icon" binding:"required"`
	RouterName string    `xorm:"varchar(50)" json:"router_name" binding:"required"`
	Path       string    `xorm:"varchar(100)" json:"path" binding:"required"`
	IsHidden   bool      `xorm:"default false" json:"is_hidden"`
	ParentId   int       `xorm:"int(11)" json:"parent_id"`
	Sort       int       `xorm:"default 0" json:"sort" `
	Component  string    `xorm:"varchar(100)" json:"component" binding:"required"`
	CreatedAt  time.Time `xorm:"created" json:"created_at"`
	UpdatedAt  time.Time `xorm:"updated" json:"updated_at"`
}

type MenuList struct {
	Id         int         `xorm:"pk autoincr" json:"id"`
	Name       string      `xorm:"varchar(50) notnull" json:"name" binding:"required"`
	Icon       string      `xorm:"default 0" json:"icon" binding:"required"`
	RouterName string      `xorm:"varchar(50)" json:"router_name" binding:"required"`
	Path       string      `xorm:"varchar(100)" json:"path" binding:"required"`
	IsHidden   bool        `xorm:"default false" json:"is_hidden"`
	ParentId   int         `xorm:"int(11)" json:"parent_id"`
	Sort       int         `xorm:"default 0" json:"sort" `
	Component  string      `xorm:"varchar(100)" json:"component" binding:"required"`
	CreatedAt  time.Time   `xorm:"created" json:"created_at"`
	UpdatedAt  time.Time   `xorm:"updated" json:"updated_at"`
	SubMenus   []*MenuList `xorm:"-"`
}

// GetMenuList 获取菜单列表
func (m *Menu) GetMenuList() (list []*MenuList, err error) {
	err = engine.Table("dts_menu").
		Where("is_hidden = ?", "0").
		Find(&list)
	log.Println(err)
	return
}

func (m *Menu) AddMenu() bool {
	if _, err := engine.Insert(m); err != nil {
		//log.Println(err)
		return false
	}
	return true
}
